<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Auth extends CI_Controller {

	public function __construct() {
		parent::__construct();
		
		$this->load->library('hitelesito');
		$this->load->library('template');
		$this->load->library('session');
		$this->load->helper('url');
		
		if ($this->hitelesito->is_authenticated()) {
			$data = array(
				'belepve'	=> true,
				'azonosito'	=> $this->session->userdata('azonosito'),
				'id'		=> $this->session->userdata('id'),
			);
		}
		else {
			$data = array(
				'belepve'	=> false,
			);
		}
		$this->load->vars($data);
	}

	public function index()
	{
		if ($this->hitelesito->is_authenticated()) {
			redirect('bookmark/index');
		}
	
		$this->load->helper('form');
		$this->load->library('form_validation');
		
		$szabalyok = array(
			array(
				'field' => 'azonosito',
				'label' => 'Azonosító',
				'rules' => 'trim|required|alpha_numeric',
			),
			array(
				'field' => 'jelszo',
				'label' => 'Jelszó',
				'rules' => 'trim|required',
			),
		);
		$this->form_validation->set_rules($szabalyok);
		
		if ($this->form_validation->run() == FALSE)
		{
			//$this->load->view('auth');
			$this->template->display('auth');
		}
		else
		{
			$azonosito = $this->input->post('azonosito');
            $jelszo = $this->input->post('jelszo');
            if ( $this->hitelesito->login($azonosito, $jelszo) ) {
                redirect('bookmark/index');
            } else {
				$errors = array(
					'auth_error' => 'Rossz azonosító vagy jelszó!',
				);
				$this->load->view('auth', array(
					'errors'	=> $errors,
				));
            }
		}
	}
	
	public function logout() {
		$this->hitelesito->logout();
        redirect('auth/index');
	}
}
